#!/usr/bin/perl

use Cwd 'abs_path';

use Getopt::Std;
getopt("i:o:d", \%args); 
my $input_direc = $args{i};
my $output_direc= $args{o};
my $dirtemp = $args{d}; 

#start
#use Getopt::Long;
#&Getopt::Long::GetOptions(
#'config=s'      => \$config
#);
$line = abs_path($0);
chomp $line;
@DR_array = split('/',$line);
pop(@DR_array);
pop(@DR_array);
$dir = join("/",@DR_array);
#require "$dir/bin/CONFIG.pl";
#getDetails($config);
#my $input_direc = $config{"INPUT_FOLDER"};
#my $output_direc = $config{"OUTPUT_FOLDER"};
#my $dirtemp = $config{"TEMP_FOLDER"};
print "INPUT DIREC:$input_direc\tOUTPUT DIREC:$output_direc\tTEMP DIREC:$dirtemp\n";
#end
chomp $input_direc;
chomp $output_direc;
chomp $dirtemp;
if($input_direc eq "")
{
	die "entered input directory is empty\n";
	
}
if($output_direc eq "")
{
        die "entered output directory is empty\n";

}
if($dirtemp eq "")
{
        die "entered temporary directory is empty\n";

}

@arr_dir = split("",$input_direc);
$arr_d = @arr_dir;
if($arr_dir[$arr_d-1] eq "/")
{
	$arr_dir[$arr_d-1] = "";
}
$input_direc = join('',@arr_dir);

@arr_dir = split("",$output_direc);
$arr_d = @arr_dir;
if($arr_dir[$arr_d-1] eq "/")
{
        $arr_dir[$arr_d-1] = "";
}
$output_direc = join('',@arr_dir);

$round = sprintf("%.0f", rand()*time());
#$line = abs_path($0);
#chomp $line;
#@DR_array = split('/',$line);
#pop(@DR_array);
#pop(@DR_array);
#$dir = join("/",@DR_array);
$output1 = $round."output1";
$output2 = $round."output2";
#$output3 = $round."output3";
#system("mkdir $dirtemp");
system("mkdir -p $dirtemp");
system("mkdir -p $output_direc");
#qc start
$input_dir = $round."_input";
$qcdir = $round."sampleqc";
system("mkdir $dirtemp/$input_dir");
system("mkdir $dirtemp/$qcdir");

system("cp $input_direc/* $dirtemp/$input_dir");
system("gunzip $dirtemp/$input_dir/*");
#script start
$filename_ind = $round."filename.txt";
system("ls $dirtemp/$input_dir > $dirtemp/$filename_ind");
#open(BUFF,"<$dir/filename.txt");
open BUFF, "< $dirtemp/$filename_ind" or die "Can't open $dirtemp/$filename_ind : $!";
@output=<BUFF>;
system("rm -rf $dirtemp/$filename_ind");



#Converting each binary cel file to ascii format
for($i=0;$i<@output;$i++)
{
        #print "converting file " . $output[$i] . "\n";
        if($output[$i] =~ m/CEL/i)
        {
                chomp($output[$i]);
                my $cmd = "$dir/bin/bin_HGUA/apt/bin/apt-cel-convert -f text -o $dirtemp/$qcdir  $dirtemp/$input_dir/" . $output[$i];
                #print "$cmd\n";
                system($cmd);
        }
}
#system("rm -rf $dirtemp/$qcdir/apt-cel-convert.log");
#script end
system("$dir/bin/Rscript $dir/bin/rscript_barcode_presentabsent.R $dirtemp/$qcdir/ $dir/bin/bin_HGUA/rlib"); 
system("perl $dir/bin/perl_barcode.pl  $dirtemp/$qcdir/barcode.txt > $dirtemp/$qcdir/result_barcode");
#die;
system("perl $dir/bin/perl_frma.pl  $dirtemp/$qcdir/frma.txt $dir/bin/spike_probesets.txt> $dirtemp/$qcdir/result_spikein");
system("perl $dir/bin/perl_presentabsent.pl  $dirtemp/$qcdir/present_absent.txt > $dirtemp/$qcdir/result_present_absent"); 
system("mkdir $dirtemp/$qcdir/temp");
system("$dir/bin/Rscript $dir/bin/frmascript.R $dirtemp/$qcdir $dir/bin/bin_HGUA/rlib");
system("mv $dirtemp/$qcdir/result.txt $dirtemp/$qcdir/result_mad");
$sys = "perl $dir/bin/perl_calc_percentile.pl $dirtemp/$qcdir/result_barcode $dirtemp/$qcdir/result_spikein $dirtemp/$qcdir/result_mad $dirtemp/$qcdir/result_present_absent $dir/bin/HGUPLUS2_sample_QA $dirtemp/$qcdir/sample_qc_output";
print $sys."\n";
system($sys);
#filtering the input samples based on QC
open(BUFF,"$dirtemp/$qcdir/sample_qc_output") or die " no sample_qc_output file exists\n";
$line = <BUFF>;
while(<BUFF>)
{
	@array = split("\t",$_);
	if($array[1] ne "Y" )
	{	
		if(-e "$dirtemp/$input_dir/$array[0]")
		{
			system("rm $dirtemp/$input_dir/$array[0]");
		}
	}
}
system("mv $dirtemp/$qcdir/sample_qc_output $output_direc/sample.qc");
system("rm -rf $dirtemp/$qcdir/*");
system("rm -rf $dirtemp/$qcdir");
#qcend
system("mkdir $dirtemp/$output1");
system("mkdir $dirtemp/$output2");




#system("mkdir $dir/temp/$output3");
$sys = "perl $dir/bin/bin_HGUA/batch_normalized_frma_convertedCEL2.pl -i $dirtemp/$input_dir -o $dirtemp/$output1 -d $dirtemp";
print $sys."\n";
system($sys);
system("perl $dir/bin/bin_plus2/batch_normalized_frma_convertedCEL2.pl -i $dirtemp/$input_dir -o $dirtemp/$output2 -d $dirtemp");
open(BU,"$dirtemp/$output1/filenames");
#open(BU1,"$dir/bin/common_genes_hthguA_hguA_plus2.txt");
#open(BU2,"$dir/bin/common_genes_hguB_plus2_notin1.txt");
=head
open(BU1,"$dir/bin/common_genes_hguA_hguB.txt");
while($line = <BU1>)
{
	chomp($line);
	@arra = split(/\t/,$line);
	$hash{$arra[1]} = "A";
}
=cut
open(BU1,"$dir/bin/bora_list_build37_v1");
while($line = <BU1>)
{
        chomp($line);
        $hash{$line} = "A";
}

#while($line = <BU1>)
#{
#        chomp($line);
#        @arra = split(/\t/,$line);
#        $hash{$arra[1]} = "A";
#}
#$ntest = 0;
while($line = <BU>)
{
	chomp($line);
	#print $line."\n";
	open BUB1,"< $dirtemp/$output1/$line" or die "no file $dirtemp/$output1/$line";
	open BUB2,"< $dirtemp/$output2/$line" or die "no file $dirtemp/$output2/$line";
	open(WRB,">$output_direc/$line");
	$li1 = <BUB1>;
	print WRB $li1;
	$ntest = 0;
	while($li1 = <BUB1>)
	{
		chomp($li1);
		@arry1 = split(/\t/,$li1);
		$arry1[0] =~ s/_at$//g;
		$li1 = join("\t",@arry1);
		if($hash{$arry1[0]} eq "A")
		{
			$hashy{$arry1[0]}  = "A";
			print WRB $li1."\n";
			$ntest++;
		}	
	}	
	print "$line\t$ntest\t";
	while($li1 = <BUB2>)
        {
                chomp($li1);
                @arry1 = split(/\t/,$li1);
		$arry1[0] =~ s/_at$//g;
		$li1 = join("\t",@arry1);
                if($hash{$arry1[0]} eq "A" && $hashy{$arry1[0]} ne "A")
                {
                        print WRB $li1."\n";
			$ntest++;
                }
        }
	print "$ntest\n";
}
system("rm -rf $dirtemp/$output1/*");
system("rm -rf $dirtemp/$output2/*");
